<html>
                <head>
                <title>Login</title>
				 <link href="css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
      body {
        padding-top: 60px;
        padding-bottom: 40px;
      }
      .sidebar-nav {
        padding: 9px 0;
      }
    </style>
                </head>
                <body>
<?php
session_start();

include 'conn.php';

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    { 

        $login_user = $Mysqli->real_escape_string($_POST['gebruikersnaam']); 
        $login_pass = $Mysqli->real_escape_string(sha1($_POST['wachtwoord']));
        $user_ip = $_SERVER['REMOTE_ADDR'];
        srand ((double) microtime( )*1000000);
        $session_id = rand(1000,1000000);
 
        $q1 = "
        SELECT 
            id,
            username,
            block
        FROM 
            users
        WHERE
            username = '".$login_user."'  
        AND
            password = '".$login_pass."'
        ";

            if(!$r1 = $Mysqli->query($q1))
        {
        
            echo 'Er is een fout opgetreden!. '. $Mysqli->error;
        
        }
                                        
                                
                elseif($Mysqli->affected_rows == 1)
                {
    

                                while ($row = $r1->fetch_assoc ())
                                { 
                                
                                        $_SESSION["username"] = $row['username']; 
                                        $_SESSION['user_id'] = $row['id'];   
                                        $_SESSION['user_ip'] = $user_ip;
                                        $_SESSION['session_id'] = $session_id;
                                               
                                    if ($row['block'] == 1)
                                    {
                                        echo 'Je bent geblokkeerd, je kunt niet meer inloggen!';
                                    } 
                                        else 
                                        { 
                                            //Inloggen gelukt!!
                                            header("location: " . 'index.php?msg=succes');
                                        }

                                
    
    
                    $q2 = "
                    INSERT INTO
                        sessions
                    (
                        user_id,
                        session_id,
                        user_ip
                    )
                    VALUES
                    (
                        '".$row['id']."',
                        '".$session_id."',
                        '".$user_ip."'
                    )
                    ";
                                }

                                    if (!$Mysqli->query ($q2) )
                                    {
                                    
                                        echo '<div class="alert alert-error">Er is een fout opgetreden!'. $Mysqli->error;
										echo '</div>';
                                    }
                                



                }
                    else 
                    {   
                        echo '<div class="alert alert-error">Gebruikersnaam of wachtwoord onjuist, probeer het opnieuw!</div>'; 
                    }  
          
    } 
        else 
        {
            ?>
                

				<div class="row-fluid">
				<div class="span3"></div>
				<div class="span3">
				
                <?php 
                    if(isset($_GET['error']))
                    {
                        if($_GET['error'] == 'sess')
                        {
                            echo '<div class="alert alert-error">De sessie is ongeldig! Log aub opnieuw in!</div>';
                        }
                    } 
                ?>
                    <form class="form-horizontal" method='post'>  
						<fieldset>            
							<div class="control-group">			
							<label class="control-label" for="input01">Gebruikersnaam:</label>
                                <div class="controls">
									<input class="input-xlarge" type='text' name='gebruikersnaam' id="input01">
								</div>
                            </div>
							<div class="control-group">			
							<label class="control-label" for="input02">Wachtwoord:</label> 
                                <div class="controls">
									<input class="input-xlarge" type='password' name='wachtwoord' id="input02">  
								</div>
                            </div>
							<div class="form-actions">		
                                <button type="submit" class="btn">Login</button>
                            </div>
						</fieldset>			
                    </form>
				</div>
				</div>
				<div class="span3"></div>
				</div>

					<script src="js/jquery.js"></script>
					<script src="js/bootstrap.js"></script>
                </body>
                </html>           
        <?php
        }  
        ?>